/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-04-28
 * Time: 17:53
 */
public class test3 {
    private static final int MAX_COUNT = 6;
    private static int currentNumber = 1;
    private static final Object locker = new Object();

    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
           while(currentNumber<=MAX_COUNT){
               synchronized (locker){
                   if(currentNumber%3==1){
                       System.out.println(Thread.currentThread().getName()+":"+currentNumber);
                       currentNumber++;
                       locker.notifyAll();
                   }else{
                       try {
                           locker.wait();
                       }catch (Exception e){
                           e.printStackTrace();
                       }
                   }
               }
           }
        });


        Thread t2 = new Thread(()->{
            while(currentNumber<=MAX_COUNT){
                synchronized (locker){
                    if(currentNumber%3==2){
                        System.out.println(Thread.currentThread().getName()+":"+currentNumber);
                        currentNumber++;
                        locker.notifyAll();
                    }else{
                        try {
                            locker.wait();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }
            }
        });


        Thread t3 = new Thread(()->{
            while(currentNumber<=MAX_COUNT){
                synchronized (locker){
                    if(currentNumber%3==0){
                        System.out.println(Thread.currentThread().getName()+":"+currentNumber);
                        currentNumber++;
                        locker.notifyAll();
                    }else{
                        try {
                            locker.wait();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
        t1.setName("t1");
        t2.setName("t2");
        t3.setName("t3");
        t1.start();
        t2.start();
        t3.start();
    }
}
